<?php

namespace app\job;

use app\admin\service\WechatService;
use think\facade\Db;
use think\facade\Log;
use think\queue\Job;

/**
 * 微小媒资小程序授权
 * Class WxMediaAppletAuthorize
 * @package app\job
 */
class WxMediaAppletAuthorize
{
    private $service;

    public function __construct(
        WechatService $service
    ){
        $this->service = $service;
    }

    /**
     * 微小媒资小程序授权
     * @param Job $job
     * @param array $info
     * @return array|void
     * @throws \think\db\exception\DbException
     */
    public function fire(Job $job, array $info)
    {
        $id = $info['id'] ?? '';
        if (empty($id)) {
            $job->delete();
            return;
        }
        try {
            $thirdVideo = Db::name('third_videos')->field('id,album_id,applet_ids,video_id')
                ->where('id', $id)->where('platform', 1)
                ->find();

            if (empty($thirdVideo)) {
                $job->delete();
                return;
            }

            $applets = Db::name('applet')->where('type',1)->column('app_id', 'id');

            $origin_app_ids = !empty($thirdVideo['applet_ids']) ? json_decode($thirdVideo['applet_ids'], true) : [];
            $app_ids = array_keys($applets);

            $diff = array_diff($app_ids, $origin_app_ids);
            if (empty($diff)) {
                $job->delete();
                Db::name('third_videos')->where('id', $id)->update(['task_status' => 2]);
                return;
            }

            $app_keys = [];
            foreach ($diff as $aid) {
                $app_keys[] = $applets[$aid];
            }
            $app_keys = array_filter($app_keys);

            $result = $this->service->shortPlayAuthorize($thirdVideo['album_id'], $app_keys);
            if (!$result) {
                Log::channel('tiktok')->log('info','微小媒资授权失败,'. json_encode(['third_video_id' => $id]));
                Db::name('third_videos')->where('id', $id)->update(['task_status' => 2]);
                $job->delete();
                return;
            }

            $newAids = [];
            foreach ($applets as $k => $app_id) {
                if (in_array($app_id, $result)) {
                    $newAids[] = $k;
                }
            }

            Db::name('third_videos')->where('id', $id)
                ->update([
                    'is_authorize' => 1,
                    'applet_ids' => json_encode(array_merge($origin_app_ids, $newAids)),
                    'task_status' => 2,
                    'updated_at' => time(),
                ]);

            Log::channel('tiktok')->log('info', '微小媒资授权成功,'. json_encode(['third_video_id' => $id]));
            $job->delete();

        } catch (\Exception $e) {
            Db::name('third_videos')->where('id', $id)->update(['task_status' => 2]);
            $job->delete();
            Log::channel('tiktok')->log('error','****** ' . json_encode($info) . ' --//-- ' . '第 ' . $e->getLine() . ' 行' . '：' . $e->getMessage());
        }
    }


}